package com.epin.business.system.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.epin.base.enums.commons.StatusEnum;
import com.epin.base.exception.check.RecordQuoteException;
import com.epin.base.exception.check.RecordRepeatException;
import com.epin.base.plugins.AbstractBean;
import com.epin.base.plugins.BaseController;
import com.epin.business.system.entity.Company;
import com.epin.business.system.entity.vo.CompanyParam;
import com.epin.business.system.service.CompanyService;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author laijianbin
 * @since 2017-05-31
 */
@Controller
@RequestMapping("/company/")
public class CompanyController extends BaseController {

	@Autowired
	private CompanyService companyService;
	
	
	/**
	 * @Title: listUI
	 *
	 * @Description：列表UI访问
	 * 
	 * @param 
	 *
	 * @return String
	 *
	 */
	@RequestMapping(value = "listUI", method = RequestMethod.GET)
    public String listUI() {
		return "business/system/company/list";
    }
	
	
	/**
	 * @Title: form
	 *
	 * @Description：表格访问
	 * 
	 * @param map
	 *
	 * @return String
	 *
	 */
	@RequestMapping(value = "form")
    public String form(Map<String,Object> map) {
		return "business/system/company/form";
    }
    
	
	/**
	 * @Title: select
	 *
	 * @Description：按id查找访问
	 * 
	 * @param map, id
	 *
	 * @return String
	 *
	 */
	@RequestMapping(value = "{id}/select", method=RequestMethod.GET)
    public String select(Map<String, Object> map, @PathVariable(required = true)Integer id) {	
		
		Company company = companyService.selectById(id);
		
		map.put("company", company);
		
		return "business/system/company/edit";
    }
	
    
    /**
	 * @Title: load
	 *
	 * @Description：列表数据访问
	 * 
	 * @param pageNumber，pageSize，keyword
	 *
	 * @return Object
	 *
	 */
	@RequestMapping(value = "load")
	@ResponseBody
	public Object load(int pageNumber, int pageSize, String keyword) {
		
		pageNumber = pageNumber <= 0 ? 1 : pageNumber;
    	pageSize = pageSize <= 0 ? 10 : pageSize;
		
		Map<String, Object> params = getParams();
		
		params.put("keyword", keyword);
		
		Page<Company> result = companyService.selectCompaniesPage(pageNumber, pageSize, params);
		
		Map<String, Object> pageResponse = new HashMap<>();
		pageResponse.put("total", result.getTotal());
		pageResponse.put("rows", result.getRecords());
			
		return pageResponse;
	}
		
	
	
	/**
	 * @Title: save
	 *
	 * @Description：添加或修改访问
	 * 
	 * @param id，cParam
	 *
	 * @return AbstractBean
	 *
	 */
    @PostMapping("save")
	@ResponseBody
	public AbstractBean save(Integer id, CompanyParam cParam){
    	
    	try {
			return result(companyService.insertOrUpdateCompany(id, cParam, getOperator()));
		} catch (RecordRepeatException e) {
			return fail(e.getMessage());
		}
	}
	
	
	/**
	 * @Title: delete
	 *
	 * @Description：删除访问
	 * 
	 * @param ids
	 *
	 * @return AbstractBean
	 * 
	 */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
    public AbstractBean delete(Integer[] ids) {	
    	
		try {
			return success(companyService.deleteCompaniesByIds(Arrays.asList(ids)));
		} catch (RecordQuoteException e) {
			return fail(e.getMessage());
		}
    }
    
    
    /**
     * 
     * @Title: disable
	 *
	 * @Description：禁用访问
	 * 
	 * @param ids
	 *
	 * @return AbstractBean
     */
    @RequestMapping(value = "disable", method = RequestMethod.POST)
	@ResponseBody
	public AbstractBean disable(Integer[] ids){
    	return result(companyService.cancelCompaniesByIds(Arrays.asList(ids), StatusEnum.DISABLE, getOperator()));
    }
    
    
    /**
     * 
     * @Title: enable
	 *
	 * @Description：启用访问
	 * 
	 * @param ids
	 *
	 * @return AbstractBean
     */
    @RequestMapping(value = "enable", method = RequestMethod.POST)
	@ResponseBody
	public AbstractBean enable(Integer[] ids){
    	return result(companyService.cancelCompaniesByIds(Arrays.asList(ids), StatusEnum.ENABLE, getOperator()));
    }
	
	
    /**
     * 
     * @Title: loadList
	 *
	 * @Description：其他地方使用的分页器
	 * 
	 * @param pageNumber，pageSize，param
	 *
	 * @return Object
     */
	@RequestMapping(value = "loadList")
	@ResponseBody
	public Object loadList(int pageNumber, int pageSize, String param) {
		
		Map<String, Object> params = getParams();
		params.put("name", param);
		params.put("status", StatusEnum.ENABLE.val());
		
		Page<Company> result = companyService.selectCompaniesPage(pageNumber, pageSize, params);
		
		Map<String, Object> pageResponse = new HashMap<>();
		pageResponse.put("total", result.getTotal());
		pageResponse.put("rows", result.getRecords());
		
		return pageResponse;
	}
}
